#!/bin/bash
get_os_release() {
    local os_release result
    os_release=`grep -i '^name=' /etc/os-release | awk -F \" '{print $2}' | tr "[:upper:]" "[:lower:]"`
    if [ "X${os_release:0:6}" = "Xubuntu" ]; then
        result="ubuntu"
    elif [ "X${os_release:0:6}" = "Xcentos" ]; then
        result="centos"
    elif [ "X${os_release:0:6}" = "Xanolis" ]; then
        result="anolis"
    else
        echo "Unknown OS release"
        exit 1
    fi
    echo $result
}

[ ! -f rock-5.7.1-6.2.18-V1.1.2.aio.run ] && wget https://cancon.hpccube.com:65024/directlink/6/dtk-24.04%E9%A9%B1%E5%8A%A8/rock-5.7.1-6.2.18-V1.1.2.aio.run
chmod +x rock-5.7.1-6.2.18-V1.1.2.aio.run
echo "waiting for install  rock"
./rock-5.7.1-6.2.18-V1.1.2.aio.run
os_release=$(eval get_os_release)
case $os_release in
    "anolis" | "centos")
        echo "waiting for down load TDK"
        [ ! -f DTK-24.04-CentOS7.6-x86_64.tar.gz ] && wget https://cancon.hpccube.com:65024/directlink/1/DTK-24.04/CentOS7.6/DTK-24.04-CentOS7.6-x86_64.tar.gz
        tar zxvf DTK-24.04-CentOS7.6-x86_64.tar.gz
    ;;
    "ubuntu")
        echo "waiting for down load libdrm"
        [ ! -f libdrm-amdgpu1_2.4.91-2_amd64.deb ] && wget http://archive.ubuntu.com/ubuntu/pool/main/libd/libdrm/libdrm-amdgpu1_2.4.91-2_amd64.deb
        dpkg -i libdrm-amdgpu1_2.4.91-2_amd64.deb
        rm libdrm-amdgpu1_2.4.91-2_amd64.deb
        echo "waiting for down load TDK"
        [ ! -f DTK-24.04-Ubuntu20.04.1-x86_64.tar.gz ] && wget https://cancon.hpccube.com:65024/directlink/1/DTK-24.04/Ubuntu20.04.1/DTK-24.04-Ubuntu20.04.1-x86_64.tar.gz
        tar zxvf DTK-24.04-Ubuntu20.04.1-x86_64.tar.gz
    ;;
    esac

mv dtk-24.04 /opt/dtk
echo 'export HSA_USERPTR_FOR_PAGED_MEM=0'>>/opt/dtk/env.sh
rm  DTK-24.04*-x86_64.tar.gz rock-5.7.1-6.2.18-V1.1.2.aio.run
